$(document).ready(function(){
	
	var nameRegExp = /^[a-zA-Z0-9_]{4,16}$/ ;
	
	$("#overlogin .common-text").each(function(){
		$(this).val("");
	});
	
	$("#overlogin .short-text").each(function(){
		$(this).val("");
	});
	
	$("#overregister .common-text").each(function(){
		$(this).val("");
	});
	
	$("#overregister .short-text").each(function(){
		$(this).val("");
	});
	
	$("ul.subnav").parent().append("<span></span>");  
	$("ul.topnav li span").hover(function() {     
		$(this).parent().find("ul.subnav").slideDown('fast').show();     
		$(this).parent().hover(function() {  
		}, function(){  
			$(this).parent().find("ul.subnav").slideUp('slow');  
		});    
	}).hover(function() {  
		$(this).addClass("subhover");   
	}, function(){    
		$(this).removeClass("subhover");   
	}); 
	
	$("#loginLink").click(function(){
		overlay(0);
	});
	
	$("#registerLink").click(function(){
		overlay(1);
	});
	
	$("#closeOverlay").click(function(){
		closeOverlay();
	});
	
	$("input").each(function(){
		$(this).keypress(function(e) {
			var key = window.event ? e.keyCode : e.which;
			if (key == 13){
				return false;
			}
		});
	});
	
	
	$(".search-text").keypress(function(e){
		var keyWord = $(this).val();
		var key = window.event ? e.keyCode : e.which;
		if (keyWord != null || keyWord.trim() != "") {
			if (key == 13){
				$(this).parent().submit();
			}
		}
	});
	
	function restoreNewCategoryTable(type, name, result){
		if (!type) {
			$("#cateListTable").find("tr:last").remove();
		} else {
			var data = eval("(" + result + ")");
			$("#cateListTable input[type='text']").remove();
			if (data.flag == true ){
				if (data.id != null){
					$("#cateListTable").find("tr:last").find("td:first").html(name);
					$("#cateListTable").find("tr:last").find("td:last").html("Modify");
				} else {
					$("#cateListTable").find("tr:last").remove();
					alert("Record already exists");
				}
			} 
		}
		$("#addNewCategory").show();
	}
	
	$("#addNewCategory").click(function(){
		var table = $("#cateListTable");
		table.append(
			"<tr><td><input type=\"text\" class=\"common-text\" required=\"required\" name=\"blogCategoryName\"></td><td></td></tr>"
		);
		var lastTd = table.find("td:last");
		var addCategoryBtn = document.createElement("input");
		addCategoryBtn.value="Add";
		addCategoryBtn.type="button";
		addCategoryBtn.className = "submit-button";
		addCategoryBtn.onclick = function(){
			var cateName = $("#cateListTable input[type='text']").val();
			if(cateName != null && cateName.trim() != "") {
				$.ajax({
					url:"/manage/blog/newCategory.action",
					type:"post",
					data:{"blogCategoryName":cateName},
					error:function(data){
						alert("Could not add new blog category currently, please try again later.");
						restoreNewCategoryTable(false, cateName, data);
					},
					success:function(data){
						restoreNewCategoryTable(true, cateName, data);
					}
				});
			} else {
				alert("Category Name should not be null");
			}
		};
		
		var cancalAddBtn = document.createElement("input");
		cancalAddBtn.value = "Cancal";
		cancalAddBtn.type = "button";
		cancalAddBtn.className = "submit-reset";
		cancalAddBtn.onclick = function(){
			$("#cateListTable").find("tr:last").remove();
			$("#addNewCategory").show();
		};
		lastTd.append(addCategoryBtn);
		lastTd.append(cancalAddBtn);
		$("#addNewCategory").hide(); 
	});
	
	function restoreGuestBook() {
		$(".main-left .book-response-div").each(function(){
			$(this).remove();
		});
		
		$(".main-left .response-book").each(function(){
			$(this).show();
		});
	}
	
	$(".response-book").click(function(e) {
		restoreGuestBook();
		var parentDiv = $(this).parent();
		var previousDiv = parentDiv.prev();
		var responseDiv = document.createElement("div");
		var responseArea = document.createElement("textarea");
		var responseButton = document.createElement("input");
		
		responseButton.type="button";
		responseButton.value="Submit";
		responseButton.className="submit-button";
		responseButton.onclick = function(event){
			//Post the comment to the server and write the comment on the page
			var commentCotent = $(this).parent().find("textarea").val();
			var commentName = $(this).parent().find("input[type=text]").val();
			var commentEmail = $(this).parent().find("input[type=email]").val();
			var id = e.target.id;
			if (commentCotent == null || commentCotent.trim() == "") {
				alert("Could you please say something?");
				return;
			}
			$.ajax({
				url:"comment.action",
				type:"post",
				data:{"commentId": id, "comment":commentCotent, "postName": commentName, "email": commentEmail},
				error:function(){
					alert("Comment is currently not available, please try again later.");
					restoreGuestBook();
				},
				success:function(){
					$("a[id='"+id+"']").parent().prev().append("<div class='book-comment'>" + commentCotent +"<hr/>" + commentName+" Posted just now</div>");
					restoreGuestBook();
				}
			});
		};
		
		var responseCancel = document.createElement("input");
		responseCancel.type="button";
		responseCancel.value="Cancel";
		responseCancel.className="submit-reset";
		responseCancel.onclick = function(){
			$(".response-book").show();
			$(this).parent().remove();
		};
		
		var responseName = document.createElement("input");
		responseName.type="text";
		responseName.required = "required";
		responseName.className = "common-text";
		responseName.placeholder = "Please input your name";
		var responseEmail = document.createElement("input");
		responseEmail.type="email";
		responseEmail.required = "required";
		responseEmail.className = "common-text";
		responseEmail.placeholder = "Please input your Email";
		
		responseDiv.appendChild(responseArea);
		$("<br/><br/>").appendTo(responseDiv);
		$("<span>Email: </span>").appendTo(responseDiv);
		responseDiv.appendChild(responseEmail);		
		$("<br/><br/>").appendTo(responseDiv);
		$("<span >Name: </span>").appendTo(responseDiv);
		responseDiv.appendChild(responseName);
		$("<br/><br/>").appendTo(responseDiv);
		responseDiv.appendChild(responseButton);
		var nbsp = document.createTextNode(" ");
		responseDiv.appendChild(nbsp);
		responseDiv.appendChild(responseCancel);
		responseDiv.className = "book-response-div";
		
		previousDiv.append(responseDiv);
		
		$(this).hide();
	});
		
	function overlay(type) {
		var overlay = $("#overlay");
		overlay.fadeIn(500);
		var overholder = $("#overholder"); 
		var left = $(window).width() / 2 - overholder.width() / 2;
		var top = $(window).height() / 2 - overholder.height() / 2;
		overholder.css("top", top);
		overholder.css("left", left);
		overholder.show();
		if (type == 0) {
			$("#overlogin").show();
		} else {
			$("#overregister").show();
		}
	}
	
	function closeOverlay(){
		$("#overlogin").hide();
		$("#overlogin .search-text").each(function(){
			$(this).val("");
		});
		$("#overregister").hide();
		$("#overregister .search-text").each(function(){
			$(this).val("");
		});
		$("#overlay").fadeOut(500);
	}
});  
